package ipebg.mzk.facade.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


@Entity
@Table(name = "MS_USER")
public class MsAdminUser implements Serializable,UserDetails {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4669941078808234581L;
	@Id
	@GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
	@GeneratedValue(generator = "hibernate-uuid")
	@Column(name = "ADMINID")
	private String adminId;
	@Column(name = "LOGINNAME")
	private String loginName;
	@Column(name = "REALNAME")
	private String realName;
	@Column(name = "PASSWORD")
	private String password;
	@Column(name = "PHONE")
	private String phone;
	@Column(name = "TELEPHONE")
	private String telePhone;
	@Column(name = "EMAIL")
	private String email;
	@Column(name = "ENABLED")
	private String enabled;
	@Column(name = "CREATEDTIME")
	private Date createdTime;
	@Column(name = "EDITTIME")
	private Date editTime;
	@Column(name = "DESCRIPTION")
	private String description;
	@Column(name = "ORG_ID")
	private String orgId;//部門
	@Column(name = "DOMINATE")
	private String dominate;//部門
	
	// 多对多雙向
	@ManyToMany(targetEntity=MsAdminRole.class,fetch=FetchType.EAGER)
	@JoinTable(name = "MS_USERROLE", joinColumns={@JoinColumn(name="ADMINID")},inverseJoinColumns={@JoinColumn(name="ROLEID")})
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	public List<MsAdminRole> roleList = new ArrayList<MsAdminRole>();

	public String getAdminId() {
		return adminId;
	}

	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getTelePhone() {
		return telePhone;
	}

	public void setTelePhone(String telePhone) {
		this.telePhone = telePhone;
	}

	public String getEmail() {
		return email==null?"":email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getEnabled() {
		return enabled;
	}

	public void setEnabled(String enabled) {
		this.enabled = enabled;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public Date getEditTime() {
		return editTime;
	}

	public void setEditTime(Date editTime) {
		this.editTime = editTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<MsAdminRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<MsAdminRole> roleList) {
		this.roleList = roleList;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getDominate() {
		return dominate;
	}

	public void setDominate(String dominate) {
		this.dominate = dominate;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		for (int i = 0; i < roleList.size(); i++) {
			grantedAuthorities.add(new SimpleGrantedAuthority(roleList.get(i).getRoleCode()));
		}
		grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_NORMAL_SIGN_USER"));
		grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
		return grantedAuthorities;
	}

	@Override
	public String getUsername() {
		return this.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled.equalsIgnoreCase("false")?false:true;
	}


	

}
